
import {
  TextureLoader,
  Color,
  Path,
  AmbientLight,
  DirectionalLight,
  PointLight,
  Group,
  RepeatWrapping,
  Vector3,
  PerspectiveCamera,
  OrthographicCamera,
  Box3,
  MeshStandardMaterial,
  MeshMatcapMaterial,
  MeshLambertMaterial,
  MeshBasicMaterial,
  MeshToonMaterial,
  MeshPhongMaterial,
  MeshPhysicalMaterial,
  Mesh,
  PCFSoftShadowMap,
  WebGLRenderer,
  Cache,
  PlaneGeometry,
  Fog,
  Scene
} from 'three';

import FontSystem from './font-system.js';

class LightSystem extends FontSystem {
  constructor(options = {}) {
    super(options);
    this.lights = new Map(); // 存储光源实例
  }

  /**
   * 通用光源创建方法（支持扩展）
   * @param {Object} config - 光源配置（JSON结构）
   */
  setupLights(config) {

    const scene = this.scene;
    this.clearLights(); // 清空旧光源
    ;
    // 点光源（适配GIMP立体字）

    if (config.lights) {
      for (var light of config.lights) {
        if (light.type == "pointLight") {
          const pl = light;
          const pointLight = new PointLight(
            new Color(pl.color || '#FFFFFF'), // 默认白色
            pl.intensity ?? 1, // 强度默认1
            pl.distance ?? 0, // 衰减距离
            pl.decay ?? 1 // 衰减系数
          );
          pointLight.position.set(pl.position.x, pl.position.y, pl.position.z);
          this.scene.add(pointLight);
          this.lights.set('point', pointLight);
        } else if (light.type == "directionalLight") {
          const dl = light;
          const dirLight = new DirectionalLight(
            new Color(dl.color || '#FFFFFF'),
            dl.intensity ?? 1
          );
          dirLight.position.set(dl.position.x, dl.position.y, dl.position.z);
          dirLight.castShadow = dl.castShadow ?? true; // GIVR需要投影
          this.scene.add(dirLight);
          this.lights.set('directional', dirLight);
        } else if (light.type == "ambientLight") {
          const al = light;
          const ambientLight = new AmbientLight(
            new Color(al.color || '#404040'),
            al.intensity ?? 0.5
          );
          this.scene.add(ambientLight);
          this.lights.set('ambient', ambientLight);
        }
      }
    } else {
      if (config.pointLight?.enabled) {
        const pl = config.pointLight;
        const pointLight = new PointLight(
          new Color(pl.color || '#FFFFFF'), // 默认白色
          pl.intensity ?? 1, // 强度默认1
          pl.distance ?? 0, // 衰减距离
          pl.decay ?? 1 // 衰减系数
        );
        pointLight.position.set(pl.position.x, pl.position.y, pl.position.z);
        this.scene.add(pointLight);
        this.lights.set('point', pointLight);
      }

      // 方向光（适配GIVR线条阴影）
      if (config.directionalLight?.enabled) {
        const dl = config.directionalLight;
        const dirLight = new DirectionalLight(
          new Color(dl.color || '#FFFFFF'),
          dl.intensity ?? 1
        );
        dirLight.position.set(dl.position.x, dl.position.y, dl.position.z);
        dirLight.castShadow = dl.castShadow ?? true; // GIVR需要投影
        this.scene.add(dirLight);
        this.lights.set('directional', dirLight);
      }

      // 环境光补充（GIVR黑背景必备）
      if (config.ambientLight?.enabled) {
        const al = config.ambientLight;
        const ambientLight = new AmbientLight(
          new Color(al.color || '#404040'),
          al.intensity ?? 0.5
        );
        this.scene.add(ambientLight);
        this.lights.set('ambient', ambientLight);
      }

    }

  }

  // 清空光源（切换配置时调用）
  clearLights() {
    this.lights.forEach(light => this.scene.remove(light));
    this.lights.clear();
  }
}


export default LightSystem;